<template>
  <div>
    <CustomDialog
      title="新增/编辑会员成员"
      width="900px"
      :visible.sync="isShowDialog"
      @closed="handleCloseDialog"
    >
      <el-form
        ref="ruleForm"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-position="left"
        hide-required-asterisk
      >
        <div class="h1 mt-0">成员信息</div>
        <!-- 成员名称 -->
        <el-form-item label="" prop="name" class="row-left">
          <template #label>
            <div class="form-label required">成员名称</div>
          </template>
          <el-input v-model="form.name" placeholder="请输入"></el-input>
        </el-form-item>
        <!-- 成员类型 -->
        <el-form-item prop="type" class="row-right">
          <template #label>
            <div class="form-label required">成员类型</div>
          </template>
          <el-select v-model="form.type" class="w-full">
            <el-option
              v-for="item in memberTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 成员头像 -->
        <el-form-item label="" prop="imgList" class="row-all">
          <template #label>
            <div>成员头像</div>
          </template>
          <CustomUpload :img-list.sync="form.imgList" />
        </el-form-item>
        <!-- 联系方式 -->
        <el-form-item label="" prop="phone" class="row-left">
          <template #label>
            <div class="form-label required">联系电话</div>
          </template>
          <el-input v-model="form.phone" placeholder="请输入"></el-input>
        </el-form-item>
        <!-- 微信号 -->
        <!-- <el-form-item prop="wechat" class="row-right">
          <template #label>
            <div class="form-labe">微信号</div>
          </template>
          <el-input v-model="form.wechat" placeholder="请输入"></el-input>
        </el-form-item> -->
        <!-- 成员家乡 -->
        <el-form-item label="" prop="hometown" class="row-left">
          <template #label>
            <div class="required">成员家乡</div>
          </template>
          <el-cascader
            id="district"
            ref="district"
            v-model="form.hometown"
            class="w-full"
            clearable
            filterable
            placeholder="选择区县"
            :options="areaTree"
            :props="{
              checkStrictly: true,
              multiple: false,
              value: 'code',
              label: 'name',
            }"
          ></el-cascader>
        </el-form-item>
        <!-- 邮箱地址 -->
        <el-form-item prop="email" class="row-right">
          <template #label>
            <div class="form-labe">邮箱地址</div>
          </template>
          <el-input v-model="form.email" placeholder="请输入"></el-input>
        </el-form-item>
        <!--个人介绍 -->
        <el-form-item label="" prop="desc" class="row-all mb-4">
          <template #label>
            <div class="form-label required">个人介绍</div>
          </template>
          <el-input
            v-model="form.desc"
            type="textarea"
            :rows="3"
            maxlength="300"
            show-word-limit
            placeholder="300字以内文字描述"
          ></el-input>
        </el-form-item>

        <!-- 所属企业 -->
        <div class="h1">所属企业</div>
        <!-- 企业名称 -->
        <el-form-item label="" prop="belongEnterprise" class="row-left">
          <template #label>
            <div class="form-label required">选择企业</div>
          </template>

          <el-select
            v-model="form.belongEnterprise"
            class="w-full"
            filterable
            remote
            placeholder="请输入企业名称"
            :remote-method="remoteMethod"
            :loading="nameLoading"
          >
            <el-option
              v-for="item in interpriseNameOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 企业职位 -->
        <el-form-item prop="enterprisePosition" class="row-right">
          <template #label>
            <div class="form-labe">企业职位</div>
          </template>
          <el-input v-model="form.enterprisePosition" placeholder="请输入"></el-input>
        </el-form-item>
        <!-- 备注 -->
        <div label="" class="row-left inline-block">
          <span class="text-gray-300">备注：如没有该企业，请进入会员企业添加企业</span>
          <el-button type="text" @click="handleAddEnterprise">+新增所属企业</el-button>
        </div>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button class="rounded-content-wrap" @click="isShowDialog = false">取 消</el-button>
        <el-button class="rounded-content-wrap" type="primary" @click="handleOk">确 定</el-button>
      </span>
    </CustomDialog>

    <PositionPicker
      :visible.sync="isShowMap"
      :position.sync="form.position"
      :addrs.sync="form.addrs"
    />

    <AddEditEnterprise ref="addEdit" />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import CustomUpload from '@/components/admin/manage/carrier/common/CustomUpload/index.vue'
import CustomDialog from '@/components/admin/manage/carrier/common/CustomDialog/index.vue'
import PositionPicker from '@/components/admin/manage/carrier/common/CustomAMap/PositionPicker.vue'
import { memberTypeOptions } from '@/components/admin/manage/carrier/common/dictionary'
import addEditMixin from '@/components/admin/manage/carrier/common/addEditMixin'
import AddEditEnterprise from '@/components/admin/manage/enterprise/AddEditEnterprise.vue'
import { emailPattern } from '@/utils/validate'

const selectKey = [
  { key: 'type', options: 'memberTypeOptions' },
  { key: 'belongEnterprise', options: 'interpriseNameOptions' },
]

const treeKey = [{ key: 'hometown', refName: 'district' }]

export default Vue.extend({
  components: { CustomUpload, CustomDialog, PositionPicker, AddEditEnterprise },
  mixins: [
    addEditMixin({
      treeKey,
      selectKey,
      templateName: 'B_CONSOLE_MEMBER',
      enterpriseTemplateName: 'B_CONSOLE_ENTERPRISE',
    }),
  ],
  data() {
    return {
      form: {
        name: null,
        type: null,
        imgList: [],
        phone: null,
        wechat: null,
        hometown: null,
        email: null,
        desc: null,
        belongEnterprise: null,
        enterprisePosition: null,
      },
      rules: {
        name: [{ required: true, message: '请输入成员名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择成员类型', trigger: 'change' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        hometown: [{ required: true, message: '请选择家乡', trigger: 'change' }],
        email: [{ pattern: emailPattern, message: '请输入正确的邮箱地址' }],
      },
      memberTypeOptions,
    }
  },
  methods: {
    handleAddEnterprise() {
      this.$refs.addEdit.open()
    },
  },
})
</script>

<style lang="scss" scoped>
@import '@/assets/scss/form.scss';
</style>
